package cn.itcast.test;


import cn.itcast.dao.CardDao;
import cn.itcast.dao.ClassesDao;
import cn.itcast.dao.StudentDao;
import cn.itcast.domain.Card;
import cn.itcast.domain.Classes;
import cn.itcast.domain.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MyTest02 {
    @Test
    public void findById() throws IOException {
        //读取配置文件
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        //获取操作对象
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(is).openSession();
        //获取
        CardDao cardDao = sqlSession.getMapper(CardDao.class);
        //使用
        List<Card> all = cardDao.findAll();
        //释放资源
        sqlSession.close();
        //输出
        System.out.println(all);
    }
    
    @Test
    public void testFindAllClasses() throws IOException {
        //读取配置文件
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        //获取操作对象
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(is).openSession();
        //获取
        ClassesDao classesDao = sqlSession.getMapper(ClassesDao.class);
        //使用
        List<Classes> all = classesDao.findAll();
        //释放资源
        sqlSession.close();
        //输出
        System.out.println(all);
    }

    @Test
    public void testFindAllStudent() throws Exception{
        //读取配置文件
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        //获取操作对象
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(is).openSession();
        //获取
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        //使用
        List<Student> all = studentDao.findAll();
        //释放资源
        sqlSession.close();
        //输出
        System.out.println(all);
    }
}
